package com.hup.minicontroller.server.screenImageService;

import android.graphics.Point;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceView;

import com.hup.minicontroller.server.ServerApplication;
import com.hup.minicontroller.server.mvp.IMainView;
import com.hup.minicontroller.server.service.MainService;

/**
 * 使用界面组件 {@link android.view.SurfaceView}接收录屏数据;
 * 不深究这种方式,仅供测试/体验,测试时要修改{@link MainService#SWITCH_SCREEN_ON_BOOT}=false
 * https://blog.csdn.net/l00149133/article/details/48346107
 * 观察情况:
 * .无感觉图像有延迟
 * .显示时,图像默认是不会缩小适应组件尺寸的
 * .带虚拟按键区域
 * .首次申请到权限开始录屏后,view是黑屏的;需要stop后,再次申请权限后才能显示图像(没确认是否为代码bug)
 * .下拉状态栏时,会黑屏,home后会黑屏且整个手机都会变得很卡,直到停止录像
 */
public class SurfaceViewProcessor implements IScreenDataProcessor {
    private static final String TAG = SurfaceViewProcessor.class.getSimpleName();

    public SurfaceViewProcessor(Point screenSize) {
        //缩小createVirtualDisplay的屏幕尺寸
        screenSize.x = screenSize.x / 3;
        screenSize.y = screenSize.y / 3;
    }

    @Override
    public Surface getSurface() {
        IMainView iMainView = ServerApplication.getBean(IMainView.class);
        if (iMainView == null) {
            Log.i(TAG, "getSurface: IMainView无启动");
            return null;
        }
        SurfaceView surfaceView = iMainView.switchSurfaceViewOfScreenCapture(true);
        return surfaceView.getHolder().getSurface();
    }

    @Override
    public void release() {
        ServerApplication.executeIfExist(IMainView.class, i -> i.switchSurfaceViewOfScreenCapture(false));
    }
}
